﻿using Comm.Model;
using Comm.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Comm.Web.Action
{
    /// <summary>
    /// BookComment 的摘要说明
    /// </summary>
    public class BookComment :BaseHander
    {
        BLL.BookCommentService bllbookcomment = new BLL.BookCommentService();
        BLL.Articel_WordsService bllword = new BLL.Articel_WordsService();
        public override void SonLoad()
        {
            
            string type = context.Request.Params["action"];
            switch (type) 
            {
                case "postComment":
                    GetpostComment();
                    break;
                case "load":
                    GetLoad();
                    break;
            }
        }

        public void GetpostComment() 
        {
            string msg = context.Request.Form["msg"];
            string _content = context.Request.Form["content"];
            msg = msg.Replace("<", "&lt;").Replace(">", "&gt;");//一定要过滤掉危险的字符。
            if (bllword.CheckAllForbid(msg))
            {
                context.Response.Write("err");
                return;
            }
            else if (bllword.CheckAllMod(msg))
            {
                context.Response.Write("err1");
                return;
            }
            else
            {
               msg = bllword.CheckReplaceWord(msg);
            }
            Model.BookComment model = new Model.BookComment() 
            { 
                   Msg = msg,
                   CreateDateTime = DateTime.Now,
                   BookId = Convert.ToInt32(context.Request.Form["bookId"])
            };
            bllbookcomment.Insert(model);
            context.Response.Write("yes");
        }

        public void  GetLoad()
        {
            int bookid = int.Parse(Request.Form["bookId"]);
            IList<Model.BookComment> list = bllbookcomment.QuerySingle1(bookid);
            if (list != null) 
            {
                List<Model.ModelView> newList = new List<Model.ModelView>();
                foreach (Model.BookComment model in list)
                {
                    //将原有集合中存储的评论的内容放入新的集合中。
                    ModelView viewModel = new ModelView();
                    viewModel.Msg = common.Ubb2Html(model.Msg);//对UBB编码替换成HTML代码
                    TimeSpan time = DateTime.Now - model.CreateDateTime;
                    viewModel.CreateDateTime = common.GetTimeSpan(time);//获取时间差的字符串
                    newList.Add(viewModel);
                }
                //context.Response.Write(JsonHelper.Serialize(newList.ToArray()));
                AjaxMsgHelper.AjaxMsg("ok", "加载成功！~~", JsonHelper.Serialize(newList.ToArray()));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}